package com.jisf.driver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jisf.driver.entity.po.CoachExamine;
import com.jisf.driver.entity.po.DrivingSchoolLog;
import com.jisf.driver.entity.vo.SetCoachRequestVo;
import com.jisf.driver.mapper.CoachExamineMapper;
import com.jisf.driver.mapper.ExamineMapper;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.ICoachExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisf.driver.service.IRoleMenuService;
import com.jisf.driver.utils.LogUtil;
import com.jisf.driver.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-05-10
 */
@Service
public class CoachExamineServiceImpl extends ServiceImpl<CoachExamineMapper, CoachExamine> implements ICoachExamineService {

    @Autowired
    private CoachExamineMapper coachExamineMapper;

    @Autowired
    private IRoleMenuService iRoleMenuService;

    @Autowired
    private ExamineMapper examineMapper;

    @Autowired
    private LogUtil log;

    /**
     * 为考试分配教练
     * @param setCoachRequestVo
     * @return
     */
    @Override
    public ResultCode setCoach(SetCoachRequestVo setCoachRequestVo) {
        List<Integer> coachIdVo = setCoachRequestVo.getCoachId();
        Integer examineId = setCoachRequestVo.getExamineId();

        QueryWrapper<CoachExamine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("examine_id", examineId);
        List<CoachExamine> coachExamines = coachExamineMapper.selectList(queryWrapper);
        List<Integer> coachIdPo = new ArrayList<>();
        for (CoachExamine coachExamine : coachExamines) {
            coachIdPo.add(coachExamine.getCoachId());
        }
        // 计算需要删除的教练和需要新增的教练
        iRoleMenuService.calculateInsertDelete(coachIdVo, coachIdPo);

        // 新增教练
        int insert = 0;
        if (coachIdVo.size() != 0) {
            insert = coachExamineMapper.insertCoachExamine(coachIdVo, examineId);
        }

        // 删除教练
        int delete = 0;
        if (coachIdPo.size() != 0) {
            delete = coachExamineMapper.deleteCoachExamine(coachIdPo, examineId);
        }

        // 存储日志
        String examineName = examineMapper.selectById(examineId).getName();
        String operator = UserUtil.usernameThreadLocal.get();
        String msg = String.format("用户名为%s的用户为考试场次为%s的考试安排了教练", operator, examineName);

        if (insert+delete!=0){
            String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
            log.info(drivingSchoolLog);
            return ResultCode.EXAMINE_SET_COACH_SUCCESS;
        }else {
            String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
            log.info(drivingSchoolLog);
            return ResultCode.EXAMINE_SET_COACH_FAILED;
        }
    }
}
